package com.lottery.im.module.tim.message;

import android.content.Context;
import android.text.TextUtils;
import android.util.Log;
import com.lottery.im.MyApplication;
import com.lottery.im.R;
import com.lottery.im.module.chat.adapter.ChatAdapter;
import com.tencent.imsdk.TIMCustomElem;
import com.tencent.imsdk.TIMGroupMemberInfo;
import com.tencent.imsdk.TIMMessage;
import java.io.IOException;
import org.json.JSONException;
import org.json.JSONObject;

/**
 * 自定义消息
 */
public class CustomMessage extends Message {

  private String TAG = getClass().getSimpleName();

  private final int TYPE_TYPING = 14;

  private Type type;
  private String desc;
  private String data;

  public CustomMessage(TIMMessage message) {
    this.message = message;
    TIMCustomElem elem = (TIMCustomElem) message.getElement(0);
    parse(elem.getData());
  }

  public CustomMessage(Type type) {
    message = new TIMMessage();
    String data = "";
    JSONObject dataJson = new JSONObject();
    try {
      switch (type) {
        case TYPING:
          dataJson.put("userAction", TYPE_TYPING);
          dataJson.put("actionParam", "EIMAMSG_InputStatus_Ing");
          data = dataJson.toString();
      }
    } catch (JSONException e) {
      Log.e(TAG, "generate json error");
    }
    TIMCustomElem elem = new TIMCustomElem();
    elem.setData(data.getBytes());
    message.addElement(elem);
  }

  public Type getType() {
    return type;
  }

  public void setType(Type type) {
    this.type = type;
  }

  private void parse(byte[] data) {
    type = Type.INVALID;
    try {
      String str = new String(data, "UTF-8");
      JSONObject jsonObj = new JSONObject(str);
      int action = jsonObj.getInt("userAction");
      switch (action) {
        case TYPE_TYPING:
          type = Type.TYPING;
          this.data = jsonObj.getString("actionParam");
          if (this.data.equals("EIMAMSG_InputStatus_End")) {
            type = Type.INVALID;
          }
          break;
      }
    } catch (IOException | JSONException e) {
      Log.e(TAG, "parse json error");
    }
  }

  /**
   * 显示消息
   *
   * @param viewHolder 界面样式
   * @param context 显示消息的上下文
   */
  @Override
  public void showMessage(ChatAdapter.ViewHolder viewHolder, Context context) {

  }


  /**
   * 获取消息摘要
   */
  @Override
  public String getSummary() {
    StringBuilder result = new StringBuilder();
    TIMGroupMemberInfo senderGroupMemberProfile = message.getSenderGroupMemberProfile();
    if (null != senderGroupMemberProfile) {
      // 说明这是群聊消息
      if (TextUtils.isEmpty(message.getSenderGroupMemberProfile().getNameCard())) {
        // 有自己的群名片
        result.append(message.getSenderProfile().getNickName() + "：");
      } else {
        // 无自己的群名片
        result.append(message.getSenderGroupMemberProfile().getNameCard() + "：");
      }
    }
    result.append(MyApplication.getInstance().getString(R.string.summary_file));
    return result.toString();
  }

  /**
   * 保存消息或消息文件
   */
  @Override
  public void save() {

  }

  public enum Type {
    TYPING,
    INVALID,
  }
}
